// 存储登陆名和token

import http from "@/utils/request";
import api from "@/utils/api";
import router from '@/router'
import consts from '@/utils/consts'


const EMIL = 'email'
const PASSWORD = 'password'
const TOKEN = 'token';

const state = {
    token: localStorage.getItem(TOKEN),
    email: localStorage.getItem(EMIL),
    password: localStorage.getItem(PASSWORD),
    roleCodes:[],
    moveArrays: []
};
const getters = {
    token: (state) => state.token,
    email: (state) => state.email,
    password: (state) => state.password
};
const actions = {
    login({ commit }, params) {
        const emailWithoutSuffix = params.Email
        params.Email = params.Email.indexOf(consts.emailSuffix) > -1 ? params.Email : params.Email + consts.emailSuffix
        http
            .fetchPost(api.login, params, "登录中...")
            .then(data => {
                commit('setToken', data);
                commit('setEmail', emailWithoutSuffix);
                commit('setPwd', params.Password);
                console.log("data is " + data);
                console.log('pushed to main application');
                localStorage.setItem(EMIL, params.Email);
                localStorage.setItem(PASSWORD, params.Password);
                localStorage.setItem(TOKEN, data.data);
                console.log("token is " + data.data);
                console.log(router.history.current.query.redirect,'1111111111111');
                if (router.history.current.query.redirect) {
                    router.push({ path: router.history.current.query.redirect });
                } else {
                    router.push({ path: "/navagation" });
                }
                // router.push({ path: "/main" });
            })
            .catch(err => {
                console.log("error msg is " + err)
            });
    },
    logout({ commit }) {
        commit('setToken', null);
        commit('setEmail', null);
        commit('setPwd', null);
        // localStorage.removeItem(EMIL);
        // localStorage.removeItem(PASSWORD);
        // localStorage.removeItem(TOKEN);
        router.push({ path: "/login" });
    },
};
const mutations = {
    setToken: (state, token) => {
        state.token = token;
        localStorage.setItem('token', token)
    },
    setEmail: (state, email) => {
        state.email = email;
        localStorage.setItem('email', email)
    },
    setPwd: (state, password) => {
        state.password = password;
        localStorage.setItem('password', password)
    },
    moveArrays: (state, data) =>{
        if(data.flag === 1){
            state.moveArrays.push(data.obj);
        }else if(data.flag === 2){
            state.moveArrays = data.array
        }
        
    },
    roleCodes:(state, roleCodes) =>{
        state.roleCodes = roleCodes;
    },
};
export default {
    state,
    getters,
    actions,
    mutations
}

// const user = {
//     state: {
//         token: getToken(),
//         name: '',
//     },

//     mutations: {
//         SET_TOKEN: (state, token) => {
//             state.token = token;
//             setToken(token);
//         },
//         SET_NAME: (state, name) => {
//             state.name = name;
//         },
//     },

// }

// export default user
